//tassa versiossa Calendar on muutettu luokaksi Kalenteri

package studentcalendar;

import java.util.Date;
import org.json.simple.*;

/**
 *
 * @author Samu
 */
public class Event implements JSONAware, Comparable {
    private String name, location;
    private Date startDate, endDate;

    /**
     * Constructor
     * @param name
     * @param location
     * @param startDate
     * @param endDate
     * @throws Exception
     */
    public Event(String name, String location, Date startDate, Date endDate) {
        if (startDate == null) {
            throw new NullPointerException("Start date must not be null.");
        }
        if (endDate == null) {
            throw new NullPointerException("End date must not be null.");
        }
        if (endDate.before(startDate)) {
            throw new IllegalArgumentException("End date must not be before start date.");
        }
        this.name = name != null ? name : "";
        this.location = location != null ? location : "";
        this.startDate = startDate;
        this.endDate = endDate;
    }

    public String getName() {
        return name;
    }

    public String getLocation() {
        return location;
    }

    public Date getStartDate() {
        return startDate;
    }

    public Date getEndDate() {
        return endDate;
    }

    public String toJSONString() {
        StringBuilder sb = new StringBuilder();
        
        sb.append("{\"name\":\"");
        sb.append(JSONObject.escape(name));
        sb.append("\",\"location\":\"");
        sb.append(JSONObject.escape(location));
        sb.append("\",\"start_date\":");
        sb.append(startDate.getTime());
        sb.append(",\"end_date\":");
        sb.append(endDate.getTime());
        sb.append("}");

        return sb.toString();
    }

    public int compareTo(Object o) {
        if (!(o instanceof Event)) {
            return 0;
        }
        Event other = (Event)o;
        if (other.getStartDate().equals(startDate)) {
            return 0;
        }
        return startDate.before(other.getStartDate()) ? -1 : 1;
    }
}
